﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyThietBi.BLL;
using QuanLyThietBi.DAL.Models;

namespace QuanLyThietBi
{
    public partial class QuanLyNhanVien : Form
    {
        private NhanVienBO nvBO;
        List<NHANVIEN> listNhanVien = new List<NHANVIEN>();
        private const int pagesize = 20;
        private int currentPage = 1;
        private int totalPage = 1;
        private int phongbanID = -1;

        public QuanLyNhanVien()
        {
            InitializeComponent();
        }

        private void QuanLyNhanVien_Load(object sender, EventArgs e)
        {
            BindDataToDataGridview();
            this.dtgDanhSachNhanVien.Columns["Nhân Viên ID"].Visible = false;
            BindDataToComboBoxPhongBan();
        }

        private void BindDataToDataGridview()
        {
            nvBO = new NhanVienBO();
            listNhanVien = nvBO.GetAllNhanVien();
            BindDataWithPagging(listNhanVien, phongbanID);
        }

        private void BindDataWithPagging(List<NHANVIEN> listNhanVien, int phongbanID)
        {
            int totalItems = listNhanVien.Count();
            
            totalPage = (totalItems + pagesize - 1) / pagesize;

            lblTrangHienTai.Text = "Trang " + currentPage + "/" + totalPage;

            lblTongThietBi.Text = totalItems +"";

            if (listNhanVien != null && listNhanVien.Count > 0)
            {
                DataTable table = new DataTable();
                table.Columns.Add("Nhân Viên ID", typeof(int));
                table.Columns.Add("STT", typeof(int));
                table.Columns.Add("Tên Nhân Viên", typeof(string));
                table.Columns.Add("Phòng Ban Đội", typeof(string));

                int startItem = currentPage * pagesize - pagesize;                

                //dòng này tương đương với đoạn if từ dòng 62 -> 70
                int endItem = currentPage != totalPage ? currentPage * pagesize - 1 : totalItems - 1;
                /*int endItem;
                if (currentPage != totalPage)
                {
                    endItem = currentPage * pagesize - 1;
                }
                else
                {
                    endItem = totalItems - 1;
                }*/ 

                for (int i = startItem; i <= endItem; i++)
                {
                    table.Rows.Add(listNhanVien[i].id,
                        i+1,
                        listNhanVien[i].ten_day_du,
                        GetTenPhongBanById(listNhanVien[i].phong_ban));
                        //tb.phong_ban);
                }

                this.dtgDanhSachNhanVien.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dtgDanhSachNhanVien.DataSource = table;

                if (currentPage == 1)
                {
                    btnTrangDau.Enabled = false;
                    btnTrangTruoc.Enabled = false;
                }
                else
                {
                    btnTrangDau.Enabled = true;
                    btnTrangTruoc.Enabled = true;
                }

                if (currentPage == totalPage)
                {
                    btnTrangCuoi.Enabled = false;
                    btnTrangTiep.Enabled = false;
                }
                else
                {
                    btnTrangCuoi.Enabled = true;
                    btnTrangTiep.Enabled = true;
                }
            }
        }

        private void btnThemNhanVien_Click(object sender, EventArgs e)
        {
            ThemMoiChinhSuaNhanVien add = new ThemMoiChinhSuaNhanVien();
            add.Show();
            this.Hide();
        }

        private string GetTenPhongBanById(int phongbanId)
        {
            PhongBanBO pbBO = new PhongBanBO();
            return pbBO.GetTenPhongBanById(phongbanId);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            int id = -1;
            foreach (DataGridViewRow row in this.dtgDanhSachNhanVien.SelectedRows)
            {
                DataRow tb = (row.DataBoundItem as DataRowView).Row;
                if (tb != null)
                {
                    id = Convert.ToInt32(tb.ItemArray[0].ToString());
                }
            }
            if (id != -1)
            {
                ThemMoiChinhSuaNhanVien suaForm = new ThemMoiChinhSuaNhanVien(id);
                suaForm.Show();
                this.Hide();
            }
        }

        private void btn_TroLai_Click(object sender, EventArgs e)
        {
            Main main = new Main();
            main.Show();
            this.Hide();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            currentPage = 1;
            if (string.IsNullOrEmpty(tbxTenNhanVien.Text.Trim()))
            {
                nvBO = new NhanVienBO();
                listNhanVien = nvBO.SearchNhanVien(string.Empty);
            }
            else
            {
                nvBO = new NhanVienBO();
                listNhanVien = nvBO.SearchNhanVien(tbxTenNhanVien.Text.Trim());
            }
            BindDataWithPagging(listNhanVien, phongbanID);
        }

        private void btnTrangTiep_Click(object sender, EventArgs e)
        {
            currentPage++;
            BindDataWithPagging(listNhanVien, phongbanID);
        }

        private void btnTrangTruoc_Click(object sender, EventArgs e)
        {
            currentPage--;
            BindDataWithPagging(listNhanVien, phongbanID);
        }

        private void btnTrangCuoi_Click(object sender, EventArgs e)
        {
            currentPage = totalPage;
            BindDataWithPagging(listNhanVien, phongbanID);
        }

        private void btnTrangDau_Click(object sender, EventArgs e)
        {
            currentPage = 1;
            BindDataWithPagging(listNhanVien, phongbanID);
        }

        private void BindDataToComboBoxPhongBan()
        {
            PhongBanBO phongbanBO = new PhongBanBO();
            List<PHONGBAN> dsPhongBan = phongbanBO.GetAllPhongBan();
            if (dsPhongBan != null && dsPhongBan.Count > 0)
            {
                cbxPhongBan.DataSource = dsPhongBan;
                cbxPhongBan.DisplayMember = "phong_ban";
                cbxPhongBan.ValueMember = "id";
            }
        }

        //private void cbxPhongBan_SelectionChangeCommitted(object sender, EventArgs e)
        //{
        //    currentPage = 1;
        //    if (phongbanID == -1)
        //    {
        //        nvBO = new NhanVienBO();
        //        listNhanVien = NhanVienBO.Ge
        //    }
        //}
    }
}
